package top.yjp.jtf.plugin;

import com.intellij.lexer.LexerBase;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Excel文件内容的词法分析器
 * 这是一个简单的实现，主要用于支持SyntaxHighlighter
 */
public class ExcelLexer extends LexerBase {
    // 定义一个简单的元素类型，用于表示Excel单元格内容
    public static final IElementType EXCEL_CONTENT = new ExcelElementType("EXCEL_CONTENT");
    
    private CharSequence buffer;
    private int startOffset;
    private int endOffset;
    private int currentPosition;
    private IElementType currentTokenType;

    @Override
    public void start(@NotNull CharSequence buffer, int startOffset, int endOffset, int initialState) {
        this.buffer = buffer;
        this.startOffset = startOffset;
        this.endOffset = endOffset;
        this.currentPosition = startOffset;
        advance();
    }

    @Override
    public void advance() {
        if (currentPosition >= endOffset) {
            currentTokenType = null;
            return;
        }
        
        // 简单实现：将整个内容视为一个token
        currentTokenType = EXCEL_CONTENT;
        currentPosition = endOffset;
    }

    @Override
    public int getState() {
        return 0;
    }

    @Nullable
    @Override
    public IElementType getTokenType() {
        return currentTokenType;
    }

    @Override
    public int getTokenStart() {
        return startOffset;
    }

    @Override
    public int getTokenEnd() {
        return currentPosition;
    }

    @NotNull
    @Override
    public CharSequence getBufferSequence() {
        return buffer;
    }

    @Override
    public int getBufferEnd() {
        return endOffset;
    }
}